from tinyshop_ui.common.login import common_login
from tinyshop_ui.common.add import common_add
from tinyshop_ui.common.input import common_input
from tinyshop_ui.common.iframe import common_iframe
from tinyshop_ui.common.select import common_select
from tinyshop_ui.common.findmodule import common_module
from tinyshop_ui.common.button import common_button
from selenium.webdriver.support.ui import WebDriverWait
import random

from tinyshop_ui.common.td import common_td


class market:
    def goods(self,module1,module2,parm,expect):
        webd = common_login().webd
        common_module(webd,module1,module2)
        common_add(webd)
        common_select().many_select(webd)
        common_input(webd).text(webd,parm)
        common_input(webd).checkbox()
        webd.find_element_by_class_name('button').click()
        self.marketiframe(webd)
        common_input(webd).submit()
        exp = common_td().res(webd,expect)
        webd.close()
        return exp
    def order(self,module1,module2,parm,expect):
        webd = common_login().webd
        common_module(webd,module1,module2)
        common_add(webd)
        common_select().many_select(webd)
        common_iframe(webd,parm[-1])
        common_input(webd).text(webd,parm)
        common_input(webd).checkbox()
        common_input(webd).submit()
        exp = common_td().res(webd,expect)
        webd.close()
        return exp
    def bind(self,module1,module2,parm,expect):
        webd = common_login().webd
        common_module(webd,module1,module2)
        common_add(webd)
        webd.find_element_by_class_name('button').click()
        self.bindiframe(webd)
        common_input(webd).text(webd,parm)
        exp = common_td().res(webd,expect)
        webd.close()
        return exp
    def group(self,module1,module2,parm,expect):
        webd = common_login().webd
        common_module(webd,module1,module2)
        common_add(webd)
        webd.find_element_by_class_name('button').click()
        self.marketiframe(webd)
        common_iframe(webd,parm[-1])
        common_input(webd).text(webd,parm)
        common_input(webd).checkbox()
        common_input(webd).submit()
        exp = common_td().res(webd,expect)
        webd.close()
        return exp
    def timebuy(self,module1,module2,parm,expect):
        webd = common_login().webd
        common_module(webd,module1,module2)
        common_add(webd)
        webd.find_element_by_class_name('button').click()
        self.marketiframe(webd)
        common_iframe(webd,parm[-1])
        common_input(webd).text(webd,parm)
        common_input(webd).checkbox()
        common_input(webd).submit()
        exp = common_td().res(webd,expect)
        webd.close()
        return exp
    def cash(self,module1,module2,parm,expect):
        webd = common_login().webd
        common_module(webd,module1,module2)
        common_add(webd)
        common_input(webd).text(webd,parm)
        common_input(webd).checkbox()
        common_input(webd).submit()
        exp = common_td().res(webd,expect)
        webd.close()
        return exp
    def bindiframe(self,webd):
        # WebDriverWait(webd,10).until(lambda webd:webd.find_element_by_name('Opengoods_select'))
        webd.switch_to_frame(webd.find_element_by_name('Opengoods_select'))
        check = webd.find_elements_by_xpath('//td/input')
        for i in range(2):
            index = random.randint(0,len(check))
            check[index].click()
        webd.find_element_by_class_name('button').click()
        webd.switch_to_default_content()
    def marketiframe(self,webd):
        # WebDriverWait(webd,10).until(lambda webd:webd.find_element_by_name('Opengoods_select'))
        webd.switch_to_frame(webd.find_element_by_name('Opengoods_select'))
        check = webd.find_elements_by_xpath('//td/input')
        index = random.randint(0,len(check))
        check[index].click()
        webd.find_element_by_class_name('button').click()
        webd.switch_to_default_content()